package com.diko.product.service.impl;

import com.diko.product.dao.CategoryDao;
import com.diko.product.entity.CategoryEntity;
import com.diko.product.service.CategoryBrandRelationService;
import com.diko.product.service.CategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diko.common.utils.PageUtils;
import com.diko.common.utils.Query;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        //1.查出所有分类
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        //2.组装树形的父子树形结构
        //找到所有的一级分类
        //递归查找所有子菜单

        List<CategoryEntity> level1Menus = categoryEntities.stream().filter((categoryEntity) -> {//过滤，得到一级分类
            return categoryEntity.getParentCid() == 0;//如果分类的父id==0 则返回
        }).map((menu) -> {//将上面的结果作为menu传入map函数
            //menu作为root，categoryEntities作为所有分类
            menu.setChildren(getChildren(menu, categoryEntities));
            return menu;
        }).sorted((menu1, menu2) -> {
            //menu1前面的菜单 menu2后面的菜单 对比对比排序
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return level1Menus;
    }

    @Override
    public boolean removeByMuneByIds(List<Long> asList) {
        //TODO 检查当前删除的菜单，是否被别的地方引用
        //逻辑删除
        int i=baseMapper.deleteBatchIds(asList);
        return i>0;
    }

    /**
     * 根据三级分类id，找到三级分类的完整路径
     *
     * @param catelogId 三级分类id
     * @return
     * */
    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> list = new ArrayList<>();

        //递归调用，获得所有的父分类
        List<Long> paths = findParentPath(catelogId, list);

        //逆序集合
        Collections.reverse(paths);

        return paths.toArray(new Long[paths.size()]);
    }

    @Transactional
    @Override
    public void updateCascade(CategoryEntity category) {
        this.updateById(category);
        //更新关联表
        if(!StringUtils.isEmpty(category.getName())){
            categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
        }
    }

    /**
     * 递归查询分类路径，是findCatelogPath的延申
     *
     * @param id 分类id
     * @param list 存储分类id的集合
     * @return [三级,二级,一级]
     * */
    private List<Long> findParentPath(Long id,List<Long> list){
        //存储当前分类的id
        list.add(id);
        CategoryEntity categoryEntity = this.getById(id);
        if(categoryEntity.getParentCid()!=0){
            findParentPath(categoryEntity.getParentCid(),list);
        }
        return list;
    }

    /**
     * 递归查询所有菜单的子菜单
     *
     * @param root 当前菜单
     * @param all  所有菜单
     * @return
     * */
    private List<CategoryEntity> getChildren(CategoryEntity root,List<CategoryEntity> all){
        List<CategoryEntity> children = all.stream().filter((categoryEntity) -> {
            return categoryEntity.getParentCid() == root.getCatId();
        }).map((categoryEntity) -> {
            //递归找到子菜单
            categoryEntity.setChildren(getChildren(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu1, menu2) -> {
            //菜单排序
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}